﻿using System.Globalization;
using InhollandAir.App_Code;
using System;
using System.Collections.Generic;


namespace InhollandAir
{
    public partial class Account : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var user = new User();
                DateTimeFormatInfo usDtfi = new CultureInfo("en-US", false).DateTimeFormat; //--MM/dd/yyyy
                if (Application["ActiveUsers"] != null)
                {
                    Dictionary<string, User> activeUsers = (Dictionary<string, User>)Application["ActiveUsers"];
                    var userLoggedIn = activeUsers.ContainsKey(Session.SessionID) ? user.Load(Session.SessionID, (Dictionary<string, User>)Application["ActiveUsers"]) : false;
                    if (userLoggedIn)
                    {
                        DateTime dt = DateTime.Parse(user.birthdate, usDtfi);
                        string convertedBirthdate = dt.ToString("MM-dd-yyyy");
                        
                        txt_Firstname.Text = Server.HtmlEncode(user.firstname);
                        txt_Lastname.Text = Server.HtmlEncode(user.lastname);
                        txt_Email.Text = Server.HtmlEncode(user.email);
                        txt_Address.Text = Server.HtmlEncode(user.address);
                        txt_Zipcode.Text = Server.HtmlEncode(user.zipcode);
                        txt_City.Text = Server.HtmlEncode(user.city);
                        txt_Country.Text = Server.HtmlEncode(user.country);
                        txt_Phonenr.Text = Server.HtmlEncode(user.phonenr);
                        txt_DateOfBirth.Text = Server.HtmlEncode(convertedBirthdate);
                        txt_Country.Text = Server.HtmlEncode(user.country);
                        txt_Airmiles.Text = Server.HtmlEncode(user.airmiles.ToString());
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx"); //user has to log in before checking  
                    }
                }
                else
                {
                    //log: application variable doesn't exist
                    Logging.WriteError("Application variable ActiveUsers doesn't exist");
                    Response.Redirect("/Login.aspx");
                }
            }
        }
    }
}